package tests;
import static org.junit.Assert.*;
import mailsystem.Mailbox;
import mailsystem.Message;
import mailsystem.persistance.MailsystemPersistance;

import org.junit.Test;
import static org.mockito.Mockito.*;



public class MailboxTest {
	MailsystemPersistance persistanceMock = mock(MailsystemPersistance.class);
	@Test
	public void testCheckPasscode() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		assertEquals(true, mail.checkPasscode("pass"));
	}

	@Test
	public void testAddMessage() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addMessage(new Message("Mensaje1", 0, false));
		mail.addMessage(new Message("Mensaje2", 0, false));
		mail.addMessage(new Message("Mensaje3", 0, false));
		
		assertEquals("Mensaje1", mail.getCurrentMessage().getText());
	}

	@Test
	public void testGetCurrentMessage() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addMessage(new Message("Mensaje1", 0, false));
		mail.addMessage(new Message("Mensaje2", 0, false));
		mail.addMessage(new Message("Mensaje3", 0, false));
		
		assertEquals("Mensaje1", mail.getCurrentMessage().getText());
	}

	@Test
	public void testRemoveCurrentMessage() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addMessage(new Message("Mensaje1", 0, false));
		mail.addMessage(new Message("Mensaje2", 0, false));
		mail.addMessage(new Message("Mensaje3", 0, false));
		mail.removeCurrentMessage();
		
		assertEquals("Mensaje2", mail.getCurrentMessage().getText());
	}

	@Test
	public void testSaveCurrentMessage() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addMessage(new Message("Mensaje1", 0, false));
		mail.addMessage(new Message("Mensaje2", 0, false));
		mail.addMessage(new Message("Mensaje3", 0, false));
		mail.saveCurrentMessage();
		mail.removeCurrentMessage();
		mail.removeCurrentMessage();		
		
		
		assertEquals("Mensaje1", mail.getCurrentMessage().getText());
	}
	
	@Test
	public void testNullCurrentMessage() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);			
		
		assertNull(mail.getCurrentMessage());		
	}
	
	

	@Test
	public void testSetGreeting() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.setGreeting("Chau");
		assertEquals("Chau", mail.getGreeting());
	}

	@Test
	public void testSetPasscode() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.setPasscode("123456");
		assertEquals(true, mail.checkPasscode("123456"));
	}

	@Test
	public void testGetGreeting() {
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		assertEquals("Hello", mail.getGreeting());
	}
	
	@Test
	public void whenCreateAMailBoxWeHaveZeroContacts()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		assertEquals(0, mail.getContactsCount());
	}
	
	@Test
	public void testAddContact()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		Mailbox mail2 = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addContact("2", mail2);
		assertEquals(1, mail.getContactsCount());
	}
	
	@Test
	public void testGetName()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		assertEquals("", mail.getName());
	}
	
	@Test
	public void testSetName()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.setName("Pedro");
		
		assertEquals("Pedro", mail.getName());
	}
	
	@Test
	public void testBroadCastCurrentMessage()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		Mailbox mail2 = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addContact("2", mail2);
		mail.addMessage(new Message("Mensaje1", 0, false));
		mail.broadCastCurrentMessage();
		
		assertEquals("Mensaje1", mail2.getCurrentMessage().getText());
	}
	
	@Test
	public void testBroadCastCurrentMessage2()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		Mailbox mail2 = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addContact("2", mail2);
		mail.addMessage(new Message("Mensaje1", 0, false));
		mail.saveCurrentMessage();
		mail.broadCastCurrentMessage();
		
		assertEquals("Mensaje1", mail2.getCurrentMessage().getText());
	}
	
	@Test
	public void testResendCurrentMessage()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		Mailbox mail2 = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addContact("2", mail2);
		mail.addMessage(new Message("Mensaje1", 0, false));
		String list[] = {"2"};
		mail.resendCurrentMessage(list);
		
		assertEquals("Mensaje1", mail2.getCurrentMessage().getText());
	}
	
	@Test
	public void testResendCurrentMessage2()
	{
		Mailbox mail = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		Mailbox mail2 = new Mailbox(0, "pass", "Hello", "", persistanceMock);
		
		mail.addContact("2", mail2);
		mail.addMessage(new Message("Mensaje1", 0, false));
		mail.saveCurrentMessage();
		String list[] = {"2"};
		mail.resendCurrentMessage(list);
		
		assertEquals("Mensaje1", mail2.getCurrentMessage().getText());
	}
}
